﻿using OnWork.DataCenter.BLL.HostedService;
using OnWork.DataCenter.BLL.Standard.HostedService;
using System.Text;

var builder = WebApplication.CreateBuilder(args).Inject();

// Add services to the container.
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

#region Furion配置

builder.Services.AddConsoleFormatter();
builder.Services.AddControllers().AddInject();//AddInjectWithUnifyResult();

builder.Services.AddRemoteRequest(); //远程请求HttpClient

// 文件日志配置 每天创建一个日志文件
builder.Services.AddFileLogging("logs/application-{0:yyyy}-{0:MM}-{0:dd}.log", options =>
{
    options.FileNameRule = fileName =>
    {
        return string.Format(fileName, DateTime.UtcNow);
    };
});
builder.Services.AddLogging(); //添加日志错误

#endregion Furion配置

builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();

builder.Services.AddBootstrapBlazor(null, options =>
{
    // 忽略本地化键值文化信息丢失
    options.IgnoreLocalizerMissing = true;
});

// Http请求客户端
builder.Services.AddHttpClient();

//builder.Services.AddSingleton<WeatherForecastService>();

//自动依赖注入
//builder.Services.AddServicesAutoInjectionOnWork(x => x.FullName.StartsWith("OnWork.DataCenter"));

// 增加 Table 数据服务操作类
//builder.Services.AddTableDemoDataService();

//MQTT后台任务
builder.Services.AddHostedService<MqttHostedService>();
// 定时任务
builder.Services.AddSchedule();

var app = builder.Build();

if (app.Environment.IsDevelopment())
{
    app.UseDeveloperExceptionPage();
}
else
{
    app.UseExceptionHandler("/Error");
}

//app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

#region Furion配置

//app.UseScheduleUI(); //定时任务调度ui /schedule
app.UseInject();

app.UseEndpoints(endpoints =>
{
    endpoints.MapControllerRoute(
        name: "default",
        pattern: "{controller=Home}/{action=Index}/{id?}");

    endpoints.MapBlazorHub();
    endpoints.MapFallbackToPage("/_Host");
});

#endregion Furion配置

app.Run();

//var taskbll = App.GetService<TaskSchedulerBLL>();
//taskbll.UpdateJobs();//加载配置的定时任务